package com.hogan.basiclibrary.base.baseApi.api;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hogan.basiclibrary.base.app.BaseApplication;
import com.hogan.basiclibrary.base.baseApi.interceptor.CacheInterceptor;
import com.hogan.basiclibrary.base.baseApi.interceptor.ParamsInterceptor;

import java.io.File;
import java.util.concurrent.TimeUnit;


import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

import static com.hogan.basiclibrary.base.baseApi.constant.Constant.BASE_URL;


/**
 * Created by chenhu on 2017/8/15.
 * 网络访问基类
 */
 

public class BaseHttpApiMethods {

    public static final int DEFAULT_TIME = 15;
    private Retrofit retrofit;
    private OkHttpClient.Builder httpClientBuilder;
    /**
     * 设置缓存大小为100M
     */
    private static final int CACHE_SIZE = 1024 * 1024 * 100;

    public BaseHttpApiMethods() {
        initOKHttpClient();
    }

    private static class SingHolder {
        private static BaseHttpApiMethods INSTANCE;

        public static BaseHttpApiMethods getInstance() {
            if (null == INSTANCE) {
                INSTANCE = new BaseHttpApiMethods();
            }
            return INSTANCE;
        }
    }

    public static BaseHttpApiMethods getInstance() {
        return SingHolder.getInstance();
    }

    public Retrofit getRetrofit() {
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();
        retrofit = new Retrofit.Builder()
                .client(httpClientBuilder.build())
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .baseUrl(BASE_URL)
                .build();
        return retrofit;
    }

    /**
     * 配置OKHttpClient基本属性
     */
    private void initOKHttpClient() {
        httpClientBuilder = new OkHttpClient.Builder();
        httpClientBuilder.connectTimeout(DEFAULT_TIME, TimeUnit.SECONDS);
        httpClientBuilder.readTimeout(DEFAULT_TIME, TimeUnit.SECONDS);
        httpClientBuilder.writeTimeout(DEFAULT_TIME, TimeUnit.SECONDS);
        httpClientBuilder.cache(getCache())
                //    .addInterceptor(new HeadInterceptor())//根据需要设置，别乱设置
                .addInterceptor(new ParamsInterceptor())
                .addInterceptor(getHttpLoggingInterceptor())
                .addInterceptor(getCacheInterceptor())
                .addNetworkInterceptor(getCacheInterceptor())
                .build();
    }

    /**
     * 打印网络访问日志
     */
    public static HttpLoggingInterceptor getHttpLoggingInterceptor() {
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        return loggingInterceptor;
    }

    /**
     * 缓存位置
     */
    public static Cache getCache() {
        File cacheFile = new File(BaseApplication.getInstance().getBaseContext().getExternalCacheDir(), "HoganCache");
        return new Cache(cacheFile, CACHE_SIZE);
    }

    /**
     * 缓存拦截器
     */
    public static CacheInterceptor getCacheInterceptor() {
        return new CacheInterceptor(BaseApplication.getInstance().getBaseContext(), CACHE_SIZE);
    }
}
